package bancosys.tec.security.impl.diagnostician;

import javax.security.auth.Subject;

import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;
import bancosys.tec.security.SecurityService;

/**
 * Descreve permissoes do usuario atual
 * 
 * @author takeshi
 */
public class PermissionDiagnostician extends AbstractDiagnostician {

    private final SecurityService securityService;

    /**
     * @param securityService {@link SecurityService}
     */
    public PermissionDiagnostician(SecurityService securityService) {
        this.securityService = securityService;
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return "Permissões do usuário atual";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean fillDiagnosisBean(DiagnosisBean bean) throws Exception {
        Subject subject = securityService.getCurrentThreadSubject();
        if (subject != null) {
            bean.addDescription("Public permissions for: " + subject);
            for (Object object : subject.getPublicCredentials()) {
                bean.addDescription(object.toString());
            }
            bean.addDescription("Private permissions for: " + subject);
            for (Object object : subject.getPrivateCredentials()) {
                bean.addDescription(object.toString());
            }
        } else {
            bean.addDescription("No current user found");
            return false;
        }
        return true;
    }

}
